import { getCode, getIdentity, getToken } from '@/api/public'
import { getCookies } from '@/libs/auth'
import { Toast } from 'vant'

const getDefaultState = () => {
  return {
    token: getCookies('token'),
    isRead: false, // 是否已读
    userInfo: {}, // 路由传入用户信息
  }
}

const state = getDefaultState()

const getters = {
  isRead: (state) => state.isRead,
  userInfo: (state) => state.userInfo,
}

const mutations = {
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_IS_READ(state, isRead) {
    state.isRead = isRead
  },
  SET_USER_INFO(state, userInfo) {
    state.userInfo = userInfo
  },
}

const actions = {
  changeToken({ commit }, data) {
    commit('SET_TOKEN', data)
  },
  changeIsRead({ commit }, data) {
    commit('SET_IS_READ', data)
  },
  changeUserInfo({ commit }, data) {
    commit('SET_USER_INFO', data)
  },
  getUserInfo({ commit }, code) {
    // eslint-disable-next-line no-async-promise-executor
    return new Promise(async (resolve, reject) => {
      let userInfo = {}
      // 授权登录
      if (!code) {
        // 重定向获取code
        // const returnUrl = encodeURIComponent(window.location.href.replace(/#/g, '$'))
        const returnUrl = encodeURIComponent(window.location.href)
        // await getCode({returnUrl: returnUrl})
        window.location.href =
          'http://dalianyqschool.lngov.top' +
          '/api/ac/dalianyqschool/qstbMobileService/redirectToDaLian' +
          '?returnUrl=' +
          returnUrl
      } else {
        // 获取token
        const params = {
          code: code,
          client_id: 'df91637b-a159-4726-8681-581abef5f3bf',
          client_secret: '1c8cdc4e-273b-41c3-9bb8-a955b1251bc0',
          grant_type: 'authorization_code',
          redirect_uri:
            'http://dalianyqschool.lngov.top/api/ac/dalianyqschool/qstbMobileService/redirectToDaLian',
        }
        const resToken = await getToken(params)
        if (resToken.status == 200) {
          if (resToken.data.access_token && resToken.data.user) {
            const { access_token, user } = resToken.data
            // 获取用户信息
            const resIdentity = await getIdentity(
              { access_token: access_token },
              user
            )
            if (resIdentity.status == 200) {
              // 提交保存数据
              userInfo = {
                ...resIdentity.data.data,
                card_no: resIdentity.data.data.idcard,
              }
              commit('SET_USER_INFO', userInfo)
              // 回调数据
              console.log('授权登录成功！！！！！')
              resolve(userInfo)
            } else {
              Toast('获取用户信息失败')
              reject(resIdentity.data)
            }
          } else if (resToken.data.code) {
            // 有状态码时候为失败
            Toast(resToken.data.message || resToken.data.msg)
            reject(resToken.data)
          }
        } else {
          reject(resToken)
        }
      }
    })
  },
}

export default {
  namespaced: true,
  state,
  getters,
  mutations,
  actions,
}
